package com.bianmaba.example.jpa.repository;

import com.bianmaba.example.MainApplication;
import com.bianmaba.example.jpa.bean.User;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {MainApplication.class})
@Transactional
public class UserRepositoryTester {
    @Autowired
    private UserRepository repository;
    private User user;

    @Before
    public void before() throws Exception {

        User user = new User();
        user.setUsername("username");
        user.setEmail("mail");


        this.user = repository.save(user);
        Assert.assertNotNull(this.user);
        Assert.assertNotNull(this.user.getId());
        repository.flush();
    }


    @Test
    public void findOne() {
        Optional<User> user = repository.findById(this.user.getId());
        Assert.assertNotNull(user);
    }
}
